package com.eboss2.sbs.database.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.eboss2.sbs.R;
import com.eboss2.sbs.consts.Constant;
import com.eboss2.sbs.database.DatabaseHelper;
import com.eboss2.sbs.database.table.TransDatalTable;
import com.eboss2.sbs.model.ShopDataList;
import com.eboss2.sbs.model.TransDataBean;
import com.eboss2.sbs.myapplication.MyApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Meiji on 2017/3/10.
 */

public class TransDataDao {

    private SQLiteDatabase db;

    public TransDataDao() {
        this.db = DatabaseHelper.getDatabase();
    }

    public void addInitData(List<ShopDataList> shopDataList) {
        if (shopDataList == null){
            return;
        }
//        String categoryId[] = MyApplication.applicationContext.getResources().getStringArray(R.array.trans_id);
//        String categoryName[] = MyApplication.applicationContext.getResources().getStringArray(R.array.trans_name);
//        for (int i = 0; i < 11; i++) {
//            add(categoryId[i], categoryName[i], "", Constant.NEWS_CHANNEL_ENABLE, i);
//        }
//        for (int i = 11; i < categoryId.length; i++) {
//            add(categoryId[i], categoryName[i], "", Constant.NEWS_CHANNEL_DISABLE, i);
//        }
        for (int i = 0; i < 11; i++){
            add(shopDataList.get(i).getId()+"", shopDataList.get(i).getName(), shopDataList.get(i).getValue()+"", shopDataList.get(i).getType(),Constant.NEWS_CHANNEL_ENABLE, i);
        }
        for (int i = 11; i < shopDataList.size(); i++) {
            add(shopDataList.get(i).getId()+"", shopDataList.get(i).getName(), shopDataList.get(i).getValue()+"", shopDataList.get(i).getType(), Constant.NEWS_CHANNEL_DISABLE, i);
        }
    }

    public boolean add(String channelId, String channelName, String value, String type, int isEnable, int position) {
        ContentValues values = new ContentValues();
        values.put(TransDatalTable.ID, channelId);
        values.put(TransDatalTable.NAME, channelName);
        values.put(TransDatalTable.IS_ENABLE, isEnable);
        values.put(TransDatalTable.POSITION, position);
        values.put(TransDatalTable.VALUE, value);
        values.put(TransDatalTable.TYPE, type);
        long result = db.insert(TransDatalTable.TABLENAME, null, values);
        return result != -1;
    }

    public List<TransDataBean> query(int isEnable) {
        Cursor cursor = db.query(TransDatalTable.TABLENAME, null, TransDatalTable.IS_ENABLE + "=?", new String[]{isEnable + ""}, null, null, null);
        List<TransDataBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            TransDataBean bean = new TransDataBean();
            bean.setChannelId(cursor.getString(TransDatalTable.ID_ID));
            bean.setChannelName(cursor.getString(TransDatalTable.ID_NAME));
            bean.setIsEnable(cursor.getInt(TransDatalTable.ID_ISENABLE));
            bean.setPosition(cursor.getInt(TransDatalTable.ID_POSITION));
            bean.setValue(cursor.getString(TransDatalTable.ID_VALUE));
            bean.setType(cursor.getString(TransDatalTable.ID_TYPE));
            list.add(bean);
        }
        cursor.close();
        return list;
    }

    public List<TransDataBean> queryAll() {
        Cursor cursor = db.query(TransDatalTable.TABLENAME, null, null, null, null, null, null);
        List<TransDataBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            TransDataBean bean = new TransDataBean();
            bean.setChannelId(cursor.getString(TransDatalTable.ID_ID));
            bean.setChannelName(cursor.getString(TransDatalTable.ID_NAME));
            bean.setIsEnable(cursor.getInt(TransDatalTable.ID_ISENABLE));
            bean.setPosition(cursor.getInt(TransDatalTable.ID_POSITION));
            bean.setValue(cursor.getString(TransDatalTable.ID_VALUE));
            bean.setType(cursor.getString(TransDatalTable.ID_TYPE));
            list.add(bean);
        }
        cursor.close();
        return list;
    }

    public void updateAll(List<ShopDataList> shopDataList) {
        if (shopDataList == null){
            return;
        }

        List<TransDataBean> list = queryAll();
        //判断本地是否

        for (int i = 0; i < list.size(); i++){
            updateData(shopDataList.get(i).getId()+"", shopDataList.get(i).getName(), shopDataList.get(i).getValue()+"", shopDataList.get(i).getType(),i);
        }

        for (int i = list.size(); i < shopDataList.size(); i++){
            add(shopDataList.get(i).getId()+"", shopDataList.get(i).getName(), shopDataList.get(i).getValue()+"", shopDataList.get(i).getType(), Constant.NEWS_CHANNEL_DISABLE, i);
        }




    }

    public boolean updateData(String channelId, String channelName, String value, String type, int position) {
        ContentValues values = new ContentValues();
        values.put(TransDatalTable.ID, channelId);
        values.put(TransDatalTable.NAME, channelName);
        values.put(TransDatalTable.POSITION, position);
        values.put(TransDatalTable.VALUE, value);
        values.put(TransDatalTable.TYPE, type);

        long result = db.update(TransDatalTable.TABLENAME, values, "id = ?", new String[]{channelId});

        return result != -1;
    }

    public boolean removeAll() {
        int result = db.delete(TransDatalTable.TABLENAME, null, null);
        return result != -1;
    }
}
